package main;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class FileManager {

	private List<String> files = new ArrayList<String>();
	
	private Integer maxFileSize;
	
	private static String path = "/home/luisp/Escritorio/legales/jquery/abm.js";
	
	private static final Pattern stringsToExclude = Pattern.compile("(?:^[^/]|/editor/(?:lang|plugin))");
	
	public FileManager() {
		this.maxFileSize = 300000;
//		setMaxFileSize();
//		setFilesName();
	}
	
	public void runJqueryTranslation(){
//		for (String localFile : files) {
//			FileProcessor.processFile(localFile, maxFileSize);
//		}
		
		FileProcessor.processFile(path, getMaxFileSize());
		
	}
	
	private void setFilesName() {
		try
		{
			String command;
			command="fileNames.sh";
			final Process process = Runtime.getRuntime().exec(command);
			InputStream is = process.getInputStream();
			byte[] buffer = new byte[1024];
			for(int count = 0; (count = is.read(buffer)) >= 0;){
				String s = new String(buffer, 0, count);
				Matcher matcher = stringsToExclude.matcher(s);
				if (!matcher.find()){
					files.add(s.replaceAll("(?si)\\s*$", ""));
				}
			}
			Collections.sort(files);
		}
		catch (Exception e){
			e.printStackTrace();
		}
	}

	private void setMaxFileSize(){
		try
		{
			String command;
			command="fileSize.sh";
			final Process process = Runtime.getRuntime().exec(command);
			InputStream is = process.getInputStream();
			byte[] buffer = new byte[1024];
			for(int count = 0; (count = is.read(buffer)) >= 0;){
				String s = new String(buffer, 0, count);
				maxFileSize = new Integer(s.replaceAll("(?si)\\s*$", ""));
			}
		}
		catch (Exception e){
			e.printStackTrace();
		}
	}

	public void setMaxFileSize(Integer maxFileSize) {
		this.maxFileSize = maxFileSize;
	}

	public Integer getMaxFileSize() {
		return maxFileSize;
	}

	public void setFiles(List<String> files) {
		this.files = files;
	}

	public List<String> getFiles() {
		return files;
	}
	
}
